/*
 * Created by JFormDesigner on Sun Dec 27 15:22:01 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.teacherInfo;

import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.controller.TeacherController;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.entity.Teacher;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.AdminView.teacherInfo.changes.upStudentInfo;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Br.S
 */
public class TeacherInfo extends JPanel {
    TeacherController teacherController=new TeacherController();
    public TeacherInfo() {
        initComponents();
        getdata();
    }
    public void getdata(){
        Result<List<Teacher>> teacherAll = teacherController.getTeacherAll();
        this.add(viewStudentTable(teacherAll.getData()));
        this.updateUI();
    }
    public JScrollPane viewStudentTable(List<Teacher> data){
        String[] name={"教师号:", "名字:", "年龄:", "密码:", "学校:", "电话:", "职称:", "更新时间:"};
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
        if (data==null){
            return jScrollPane( new Object[0][8],name);
        }
        Object[][] tableData=new Object[data.size()][8];
        for (int i = 0; i < data.size(); i++) {
            tableData[i][0]= data.get(i).getTeacherId();
            tableData[i][1]= data.get(i).getTeacherName();
            tableData[i][2]= data.get(i).getTeacherAge();
            tableData[i][3]= data.get(i).getTeacherPassword();
            tableData[i][4]= data.get(i).getTeacherSchool();
            tableData[i][5]= data.get(i).getTeacherPhone();
            tableData[i][6]= data.get(i).getTeacherTitle();
            tableData[i][7]= dateFormat.format(data.get(i).getUpdateTime());
        }
        return jScrollPane(tableData, name);
    }
    private  JScrollPane jScrollPane(Object[][] tableData,String[] name){
        JTable table=new JTable(tableData,name);
        table.setSize(1000,800);
        table.setBackground(Color.WHITE);
        table.setRowHeight(32);//设置行高
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        // 设置表格内容颜色
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(100);
        table.setForeground(new Color(47,53,66));                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 15));      // 字体样式
        table.setSelectionForeground(new Color(236,240,241));      // 选中后字体颜色
        table.setSelectionBackground(new Color(87,96,111));     // 选中后字体背景
        table.setGridColor(new Color(87,96,111));                     // 网格颜色
        // 设置表头

        Dimension size = table.getTableHeader().getPreferredSize();
        size.height = 40;//设置新的表头高度32
        table.getTableHeader().setPreferredSize(size);
        table.getTableHeader().setFont(new Font(null, Font.BOLD, 15));// 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.white);// 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(false);// 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);// 设置不允许拖动重新排序各列
        table.getTableHeader().setBackground(new Color(47,53,66));
        ((DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);//居中
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(770,568));
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.getHSBColor(0.0000F,0.0000F,0.9333F)));
        scrollPane.setVisible(true);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e)
            {
                if (e.getValueIsAdjusting()){
                    textField1.setText(table.getValueAt(table.getSelectedRow(), 0).toString());
                }
            }
        });
        return scrollPane;
    }

    private void button2ActionPerformed(ActionEvent e) {
        //delStudent
        Long teacherId = Long.valueOf(textField1.getText());
        Result result = teacherController.delTeacher(teacherId);
        if (result.getCode()==2000){
            JOptionPane.showConfirmDialog(null,"删除成功！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
            Container.getInstance().upDatePage();
        }else {
            JOptionPane.showConfirmDialog(null,"删除失败！", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private void button3ActionPerformed(ActionEvent e) {
        //upStudent
        JFrame jFrame = new JFrame();
        try{
            upStudentInfo upStudentInfo=new upStudentInfo(Long.valueOf(textField1.getText()),jFrame);
            upStudentInfo.setVisible(true);
            jFrame.setBounds(100,100,550,560);
            jFrame.add(upStudentInfo);
            jFrame.setVisible(true);
            Container.getInstance().upDatePage();
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null, "请先输入id");
        }

    }

    private void button4ActionPerformed(ActionEvent e) {
        //saveNewStudent
        JFrame jFrame = new JFrame();
        try{
            upStudentInfo upStudentInfo=new upStudentInfo(0L,jFrame);
            upStudentInfo.setVisible(true);
            jFrame.setBounds(100,100,550,560);
            jFrame.add(upStudentInfo);
            jFrame.setVisible(true);
            Container.getInstance().upDatePage();
        }catch (NumberFormatException numberFormatException){
            JOptionPane.showMessageDialog(null, "请先输入id");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label2 = new JLabel();
        textField1 = new JTextField();
        del = new Button(Button.CANCELBUTTON);
        up = new Button();
        button4 = new Button();

        //======== this ========
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));

            //---- label2 ----
            label2.setText("\u8bf7\u8f93\u5165");
            label2.setIconTextGap(5);
            panel1.add(label2);

            //---- textField1 ----
            textField1.setColumns(7);
            panel1.add(textField1);

            //---- del ----
            del.setText("\u4fe1\u606f\u5220\u9664");
            del.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(del);

            //---- up ----
            up.setText("\u4fe1\u606f\u4fee\u6539");
            up.addActionListener(e -> button3ActionPerformed(e));
            panel1.add(up);

            //---- button4 ----
            button4.setText("\u4fe1\u606f\u65b0\u589e");
            button4.addActionListener(e -> button4ActionPerformed(e));
            panel1.add(button4);
        }
        add(panel1, BorderLayout.NORTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label2;
    private JTextField textField1;
    private JButton del;
    private JButton up;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
